/**
 * 违章登记 列表页
 *
 * 作者 wanghw
 * 日期 2024-08-12 09:46:29
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate', 'fileUpload'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-peccancyInformation'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#peccancyInformation-table-form'),
        tableIns;

    form.verify(validate);
    form.render();
    initDate();
    initTable();

    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#laydate-range',
            range: ['#start-date', '#end-date'],
            format: 'yyyy-MM-dd',
            btns: ['now', 'confirm']
        })
        $('#end-date').val(commonJS.getCurrentDateStr(new Date()));
        $('#start-date').val(commonJS.getPastDateStr(new Date(), 7));
        form.render();

    }

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'peccancyInformationTable',
            url: ctx + 'peccancyInformation/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'informationId', title: 'id', minWidth: 120, hide: true},
                {
                    field: 'tenantId',
                    title: '公司名称',
                    minWidth: 120,
                    hide: !hasHeadCompanyRole,
                    templet: function (d) {
                        var tenant = tenantList.filter(item => item.tenantId == d.tenantId);
                        return tenant[0].tenantName;
                    }
                },
                {
                    field: 'peccancyNo',
                    title: '单号',
                    width: 175,
                    align: 'center',
                    event: 'detail',templet:function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">'+d.peccancyNo+'</p>'
                    }
                },
                {field: 'projectName', title: '项目名称', minWidth: 120},
                {
                    field: 'peccancyObject', title: '违章对象', width: 100, align: 'center', templet: function (d) {
                        if (d.peccancyObject == '1') {
                            return '<span class="layui-badge febs-tag-green">本工</span>';
                        } else if (d.peccancyObject == '2') {
                            return '<span class="layui-badge febs-tag-magenta">外包</span>';
                        } else if (d.peccancyObject == '3') {
                            return '<span class="layui-badge febs-tag-orange">外协</span>';
                        } else if (d.peccancyObject == '4') {
                            return '<span class="layui-badge febs-tag-cyan">承包队</span>';
                        } else if (d.peccancyObject == '5') {
                            return '<span class="layui-badge febs-tag-purple">部门</span>';
                        }
                    }
                },
                {
                    field: 'peccancyRealName', title: '违章对象名称', minWidth: 230, templet: function (d) {
                        var html = '';
                        if (d.peccancyObject == '4') {
                            if (d.peccancyUserContractorTeamName) {
                                html += '&nbsp;<i class="layui-icon layui-icon-team febs-blue"></i>'
                                html+=d.peccancyUserContractorTeamName
                            }
                        }else if (d.peccancyObject == '5') {
                            if (d.peccancyUserDept) {
                                html += '&nbsp;<i class="layui-icon layui-icon-idcard febs-blue"></i>'
                                html+=d.peccancyUserDept
                            }
                        } else {
                            if (d.peccancyRealName) {
                                html = '<i class="layui-icon layui-icon-user febs-blue"></i>'
                                html+=d.peccancyRealName
                            }
                            if (d.peccancyUserDept) {
                                html += '&nbsp;<i class="layui-icon layui-icon-idcard febs-blue"></i>'
                                html+=d.peccancyUserDept
                            }
                            if (d.peccancyUserPostName) {
                                html += '&nbsp;<i class="layui-icon layui-icon-creditcard febs-blue"></i>'
                                html+=d.peccancyUserPostName
                            }

                        }
                        return html
                    }
                },
                {
                    field: 'peccancyType', title: '违章类型', width: 120, align: 'center', templet: function (d) {
                        if (d.peccancyType) {
                            return febs.getDictLabel(peccancyTypeList, d.peccancyType)
                        }
                    }
                },
                {
                    field: 'peccancyRootCause', title: '违章原因', width: 140, align: 'center', templet: function (d) {
                        if (d.peccancyRootCause) {
                            return febs.getDictLabel(peccancyRootCauseList, d.peccancyRootCause)
                        }
                    }
                },
                {field: 'peccancyTime', title: '违章时间', width: 120, align: 'center'},
                {field: 'peccancyArea', title: '违章区域', minWidth: 120},
                // {field: 'invoiceRealName', title: '开单用户姓名', width: 120, align: 'center'},
                {
                    field: 'status', title: '状态', width: 100, align: 'center', templet: function (d) {
                        if (d.status == '1') {
                            return '<span class="layui-badge febs-tag-green">待提交</span>';
                        } else if (d.status == '2') {
                            return '<span class="layui-badge febs-tag-magenta">待审核</span>';
                        } else if (d.status == '3') {
                            return '<span class="layui-badge febs-tag-orange">审核退回</span>';
                        } else if (d.status == '4') {
                            return '<span class="layui-badge febs-tag-cyan">待签发</span>';
                        } else if (d.status == '5') {
                            return '<span class="layui-badge febs-tag-purple">签发退回</span>';
                        } else if (d.status == '6') {
                            return '<span class="layui-badge febs-tag-lime">已签发</span>';
                        } else if (d.status == '10') {
                            return '<span class="layui-badge febs-tag-red">已作废</span>';
                        }
                    }
                },
                {title: '操作', toolbar: '#option', align: 'center', width: 150, fixed: 'right'}
            ]],
            toolbar: '#peccancyInformation-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(peccancyInformationTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });


    table.on('toolbar(peccancyInformationTable)', function (obj) {
        //表格编辑删除事件
        var layEvent = obj.event;
        if (layEvent === 'add') {
            var tenantId = $searchForm.find('select[name="tenantId"]').val();
            febs.modal.open('新增违章记录', 'peccancyInformation/add/' + tenantId, {
                btn: ['保存', '提交',  '取消'],
                area: $(window).width() <= 750 ? ['95%', '85%'] : ['70%', '85%'],
                yes: function (index, layero) {
                    $('#peccancyInformation-edit').find('#save').trigger('click');
                },
                btn2: function (index, layero) {
                    $('#peccancyInformation-edit').find('#submit').trigger('click');
                    return false;
                },
                btn3: function (index, layero) {
                    layer.closeAll();
                },
                data: {
                    tenantId: tenantId,
                    tenantName: $searchForm.find('select[name="tenantId"]').find('option:selected').text().trim()
                }
            });
        }
    });


    table.on('tool(peccancyInformationTable)', function (obj) {
        //表格编辑删除事件
        var id = obj.data.informationId;
        var status = obj.data.status;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            if (status != '1' && status != '3') {
                febs.modal.error('提示', '该记录已提交，无法编辑', function () {
                });
                return;
            }
            febs.modal.open('修改违章记录', 'peccancyInformation/update/' + id, {
                btn: ['保存', '提交',  '取消'],
                area: $(window).width() <= 750 ? ['95%', '85%'] : ['70%', '85%'],
                yes: function (index, layero) {
                    $('#peccancyInformation-edit').find('#save').trigger('click');
                },
                btn2: function (index, layero) {
                    $('#peccancyInformation-edit').find('#submit').trigger('click');
                    return false;
                },
                btn3: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: tenantId,
                    tenantName: $searchForm.find('select[name="tenantId"]').find('option:selected').text().trim()
                }
            });
        } else if (layEvent === 'del') {
            if (status != '1' && status != '3') {
                febs.modal.error('提示', '该记录已提交，无法删除', function () {
                });
                return;
            }
            febs.modal.confirm('删除违章记录', '确定删除所选违章记录？', function () {
                deleteIds(id);
                $query.trigger('click');
            });
        } else if (layEvent === 'detail') {
            febs.modal.open('违章信息详情', 'peccancyInformation/detail/' + id, {
                area: $(window).width() <= 750 ? '90%' : ['70%', '90%'],
                shadeClose: true,
                btn: ['取消'],
                yes: function (index, layero) {
                    layer.close(index)
                },
            });
        }

    });


    function getQueryParams() {
        return {
            projectName: $searchForm.find('input[name="projectName"]').val(),
            peccancyRealName: $searchForm.find('input[name="peccancyRealName"]').val(),
            tenantId: $searchForm.find('select[name="tenantId"]').find("option:selected").val(),
            status: $searchForm.find("select[name='status']").find("option:selected").val(),
            peccancyType: $searchForm.find("select[name='peccancyType']").find("option:selected").val(),
            startDate: $searchForm.find('input[name="startDate"]').val(),
            endDate: $searchForm.find('input[name="endDate"]').val(),
            invalidate_ie_cache: new Date()
        };
    }


    function deleteIds(ids) {
        febs.get(ctx + 'peccancyInformation/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });
});